package com.victor.utils;

import com.victor.utils.wx.HttpRequestor;

import net.sf.json.JSONObject;

/**
 * 微信通过code获取openid工具类
 * 
 * @author victor
 * 
 */
public class GetOpenidUtil {

	public static String getopenid(String code) throws Exception {

		String appid = "wxf892cd9373e7de5c";
		String secret = "9c05df9f8f8ba83433959a2cdec49a56";
		String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
		// 第一次请求 获取access_token 和 openid
		String oppid = new HttpRequestor().doGet(requestUrl);
		JSONObject oppidObj = JSONObject.fromObject(oppid);
		String access_token = (String) oppidObj.get("access_token");
		String openid = (String) oppidObj.get("openid");
		String requestUrl2 = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
		String userInfoStr = new HttpRequestor().doGet(requestUrl2);
		JSONObject wxUserInfo = JSONObject.fromObject(userInfoStr);
		System.out.println(wxUserInfo);
		return openid;

	}
}
